package com.cheung.leetcode.hash;

import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * @BelongsProject : java-leetcode
 * @BelongsPackage : com.cheung.leetcode.hash
 * @Author :  cheungming
 * @CreateTime : 2024-05-06 08:49:54
 * @Description : 454
 * @Version : 1.0
 */
public class Code454Test {

    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        Map<Integer, Integer> hash = new HashMap<>();
        for (int i1 : nums1) {
            for (int i2 : nums2) {
                int key = i1 + i2;
                hash.put(key, hash.getOrDefault(key, 0) + 1);
            }
        }

        int count = 0;

        for (int i1 : nums3) {
            for (int i2 : nums4) {
                int d = -(i1 + i2);
                count += hash.getOrDefault(d, 0);
            }
        }

        return count;
    }


    @Test
    public void test1() {
        int[] nums1 = {1, 2}, nums2 = {-2, -1}, nums3 = {-1, 2}, nums4 = {0, 2};
        assert 2 == fourSumCount(nums1, nums2, nums3, nums4);
    }

    @Test
    public void test2() {
        int[] nums1 = {0}, nums2 = {0}, nums3 = {0}, nums4 = {0};
        assert 1 == fourSumCount(nums1, nums2, nums3, nums4);
    }
}
